package com.bjkcst.baselib.localconfig;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author: glc.
 * Date: 2022/5/18  11:04
 * Description:
 */
public final class LocalIniUtil {
    private static final String TAG = "LocalIniUtil";

    /*** 默认的配置文件夹 */
    private static final String INIDIR = "/KingKong/app/";

    /*** 默认的配置文件名 */
    public static final String INIFILE_DEFAULT = "kcst.ini";

    /*** 默认的Select */
    public static final String SELECT_DEFAULT = "APP";

    /*** Config Select */
    public static final String SELECT_CONFIG = "CONFIG";

    private volatile static LocalIniUtil mInstance;

    private File iniFile;
    private IniFileUtil mIniFileUtil;


    private LocalIniUtil() {
        mIniFileUtil = new IniFileUtil();
    }

    public static LocalIniUtil getInstance() {
        if (mInstance == null) {
            synchronized (LocalIniUtil.class) {
                if (mInstance == null) {
                    mInstance = new LocalIniUtil();
                }
            }
        }
        return mInstance;
    }

    public LocalIniUtil init(String path) {
        String filePath = Environment.getExternalStorageDirectory().getPath() + INIDIR;
        File iniDir = new File(filePath);
        if (!iniDir.exists()) {
            iniDir.mkdirs();
        }
        //本地配置文件
        iniFile = new File(filePath + path);
        if (!iniFile.exists()) {
            // 初始化ini配置文件
            try {
                iniFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        mIniFileUtil.load(iniFile);
        return mInstance;
    }

    /**
     * 写入数据到本地文件的指定节点
     *
     * @param selectTag 节点
     * @param map       数据
     * @param isCover   是否覆盖
     * @return mInstance
     */
    public LocalIniUtil setDefaultParam(String selectTag, Map<String, String> map, boolean isCover) {
        if (TextUtils.isEmpty(selectTag) || map == null || map.isEmpty()) {
            return mInstance;
        }
        map.entrySet().forEach(entry -> mIniFileUtil.set(selectTag, entry.getKey(), entry.getValue(), isCover));
        mIniFileUtil.save(iniFile);
        return mInstance;
    }


    /**
     * 按指定顺序写入数据到本地文件的指定节点
     *
     * @param selectTag 节点
     * @param order     指定顺序
     * @param map       数据
     * @param isCover   是否覆盖
     * @return mInstance
     */
    public LocalIniUtil setDefaultParam(String selectTag, List<String> order, Map<String, String> map, boolean isCover) {
        if (TextUtils.isEmpty(selectTag) || (order == null || order.size() == 0) || (map == null || map.isEmpty())) {
            return mInstance;
        }
        for (String key : order) {
            mIniFileUtil.set(selectTag, key, map.get(key), isCover);
        }
        mIniFileUtil.save(iniFile);
        return mInstance;
    }

    /**
     * 写入数据到本地文件的指定节点
     *
     * @param selectTag 节点
     * @param key       数据
     * @param value     值
     * @param isCover   是否覆盖
     * @return mInstance
     */
    public LocalIniUtil setDefaultSingleParam(String selectTag, String key, Object value, boolean isCover) {
        if (TextUtils.isEmpty(selectTag) || TextUtils.isEmpty(key) || value == null) {
            return mInstance;
        }
        mIniFileUtil.set(selectTag, key, value, isCover);
        mIniFileUtil.save(iniFile);
        return mInstance;
    }

    public int loadIntData(String selectTag, String key) {
        mIniFileUtil.load(iniFile);
        return mIniFileUtil.getInt(selectTag, key);
    }

    public String loadStrData(String selectTag, String key) {
        mIniFileUtil.load(iniFile);
        return mIniFileUtil.getString(selectTag, key);
    }

    public HashMap<String, String> loadMapData(String selectTag, HashMap<String, String> map) {
        if (TextUtils.isEmpty(selectTag) || map == null || map.isEmpty()) {
            return new HashMap<>();
        }
        mIniFileUtil.load(iniFile);
        map.forEach((key, value) -> map.put(key, mIniFileUtil.getString(selectTag, key)));
        return map;
    }
}
